package me.xplabs.engine.time 
{
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class TimePieceNode 
	{
		public var gap:int;
		public var func:Function;
		public var endFunc:Function;
		public var key:int;
		
		private var _last:int;
		private var _isRepect:Boolean;
		private var _repect:int;
		private var _per:int;
		private var _isJumpPer:Boolean;
		private var _curframe:uint;
		private var _curTime:uint;
		public function TimePieceNode(pkey:int, pnow:int, pgap:Number, pfunc:Function, pendFunc:Function=null, ptime:int = -1, pisJumpPer:Boolean = true) 
		{
			key = pkey;
			gap = pgap * 1000;
			func = pfunc;
			endFunc = pendFunc;
			_isRepect = ptime == -1;
			_repect = _isRepect? 1:ptime;
			_last = pnow + gap;
			_isJumpPer = pisJumpPer;
			_curframe = 0;
		}
		public function dispose():void
		{
			if (endFunc != null) endFunc(key);
		}
		
		public function update(pgap:int):Boolean
		{
			if (_repect <= 0) return true;
			_curframe += pgap;
			if (_curframe < gap) return false;
			var tempGap:int = _curframe/gap;
			_curframe -= tempGap * gap;
			_repect--;
			if (func != null)
			{
				if (func.length == 0) func();
				else if (func.length == 1) func(_repect);
				else if (func.length == 2) func(_repect, key);
			}
			if (_isRepect || _repect > 0) return false;
			return true;
		}
	}

}